package com.bank.controller;

import com.bank.entity.User;
import com.bank.exception.AuthenticationException;
import com.bank.model.LoginResult;
import com.bank.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2016/5/17.
 */
@RestController
@RequestMapping(value = "user")
public class LoginUserController extends  WebApiController{
    @Resource
    protected UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public LoginResult login(String username, String password) {
        User user = userService.findUserByUserName(username);
        if (password.equals(user.getPassword())) {
            return new LoginResult("success", user.getKeyId());
        } else {
throw new AuthenticationException(403, "登陆失败");
          }
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public LoginResult register(String username, String password, String phone,
                                String date, String username_pinyin, String address,
                                String ID_number,  String  sex) {
        User user = new User();
        user.setDate(date);
        user.setPassword(password);
        user.setUsername(username);
        user.setPhone(phone);
        user.setUsername_pinyin(username_pinyin);
        user.setAddress(address);
        user.setID_number(ID_number);
        user.setSex(sex);
        userService.save(user);
        return new LoginResult("success", user.getKeyId());
    }
}
